// pages/myActiveXq/myActiveXq.js
const app = getApp()
var url = require('../../utils/url.js');
var orderId
var token;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listItem: [],
    adrInfo: "",
    row: "",
    is_award: "",
    award: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    orderId = options.id;
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        token = res.data;
        that.getInfo();
      },
    })

  },
  getInfo() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/exchange/info',
      data: { id: orderId },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        that.setData({
          row: res.data.data,
          adrInfo: res.data.data.address
        });
      }
    });
  },
  toCencel: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '温馨提示',
      content: '您确定取消订单吗',
      showCancel: true,
      cancelText: '我再想想',
      cancelColor: '',
      confirmText: '确定',
      confirmColor: '#DD2626',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中..',
            mask: true,
          })
          wx.request({
            url: url.URl + 'my/order/cancel',
            data: { id: id },
            method: 'post',
            header: {
              'Authorization': token
            },
            success: function (res) {
              console.log(res)
              wx.hideLoading()
              var msg = res.data.msg
              if (res.data.code == 200) {
                wx.showToast({
                  title: msg,
                  icon: 'none',
                  duration: 2000,
                  success: function () {
                    setTimeout(() => {
                      // 模拟请求数据，并渲染
                      that.getInfo()
                      // 数据成功后，停止下拉刷新
                    }, 1000);
                  }
                })
              } else {
                wx.showToast({
                  title: msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      },

    })
  },
  tobuy: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/tobuy',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if (res.data.code == 201) {


        } else {
          wx.requestPayment({
            'appId': res.data.appId,
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              that.setData({
                disabled: false
              })
              console.log(res)
              wx.showToast({
                title: "支付成功",
                icon: 'success',
                duration: 2000,
                success: function () {

                }
              })
            },
            fail: function () {


            }
          })
        }
      }
    })
  },
  remind: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/remind',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })
  },
  confirm: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/exchange/confirm',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        that.getInfo()
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })
  },
  del: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/del',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })
  },
  returns: function (e) {
    var that = this;
    var item = e.currentTarget.dataset.item;
    var jsonItem = JSON.stringify(item);
    wx.navigateTo({
      url: '/pages/myReturn/myReturn?item=' + jsonItem,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  evaluation: function (e) {
    var that = this;
    var item = e.currentTarget.dataset.item;
    var jsonItem = JSON.stringify(item);
    wx.navigateTo({
      url: '/pages/myevaluation/myevaluation?item=' + jsonItem,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})